<%@ include file="css/header.jsp" %>
<title>Add user</title>
<%
String userId;
String manageUsers;
if((String)session.getAttribute("userId")==null||((String)session.getAttribute("userId")).trim().length() == 0) userId = "null";
else userId = (String)session.getAttribute("userId");
if((String)session.getAttribute("manageUsers")==null||((String)session.getAttribute("manageUsers")).trim().length() == 0) manageUsers = "null";
else manageUsers = (String)session.getAttribute("manageUsers");
	if(!(userId.equals("null"))){
		if(manageUsers.equals("1")){
%>
<script type="text/javascript">
$(document).ready(function() {
	 var pageObj = $("#AddAdmin");
     var formObj = pageObj.find("#formAddAdmin");
     /**
      * Init Control
      */
     //Form
     formObj.validationEngine('attach');
	$('#mnu li').hover(
	        function () {
	            //show its submenu
	            $('ul', this).slideDown(100);
	 
	        }, 
	        function () {
	            //hide its submenu
	            $('ul', this).slideUp(100);         
	        }
	    );

	$.ajax({
        type: 'POST',
        url: '<%=request.getContextPath()%>/AddAdminServlet/LoadDdl',
        dataType: 'json',
        success: function(data){
            	bindJsonToHtml(data, pageObj);
        },
        error: function(){
            $.msgbox('error', {
                type : 'error'
            }, function(buttonPressed) {
            });
        }
    });
    
	$("#btnSave").click(function(){
		var formObj = $("#formAddAdmin");
		var checkUser = formObj.find("#checkUser").text();
		var data = formObj.serialize();
		if(formObj.validationEngine('validate')) {
				if(checkUser==""){
					$.ajax({
			            type: 'POST',
			            url: '<%=request.getContextPath()%>/AddAdminServlet/AddNewUser',
			            dataType: 'json',
			            data: data,
			            success: function(data){
			                $.each(data, function(key, value) {
			                    //Validate is error send?
			                    if (key == 'error') {
			                        //alert(value);
			                        $.msgbox(value , {
			                            type : 'error'
			                        }, function(buttonPressed) {
			                            // your code here
			                        });
			                    } else { //No Error
			                    	window.location.href = "ManageAdminServlet";
			                    }
			                    return false; //break each loop
			                });
			            },
			            error: function(){
			                $.msgbox('Error', {
			                    type : 'error'
			                }, function(buttonPressed) {
			                    // your code here
			                });
			            }
			        });
				}
			
		}
        return false;
    });
});   

</script>



<body>
<script type="text/javascript">
function checkUsername(){
	var username = $("#username").val();
	$.ajax({
        type: 'POST',
        url: '<%=request.getContextPath()%>/AddAdminServlet/CheckUsername?username='+username ,
        dataType: 'json',
        success: function(data){
            $.each(data, function(key, value) {
                //Validate is error send?
                if (key == 'error') {
                   $("#checkUser").text("Username already exists.");
                } else { 
                	 $("#checkUser").text("");
                }
                return false; //break each loop
            });
        },
        error: function(){
            $.msgbox('Error', {
                type : 'error'
            }, function(buttonPressed) {
                // your code here
            });
        }
    });
}
</script>
<div id="jp-main">
        <div class="jp-sheet">
            <div class="jp-sheet-tl"></div>
            <div class="jp-sheet-tr"></div>
            <div class="jp-sheet-bl"></div>
            <div class="jp-sheet-br"></div>
            <div class="jp-sheet-tc"></div>
            <div class="jp-sheet-bc"></div>
            <div class="jp-sheet-cl"></div>
            <div class="jp-sheet-cr"></div>
            <div class="jp-sheet-cc"></div>
            <div class="jp-sheet-body">
                <div class="jp-header">
                    <div class="jp-header-center">
                        <div class="jp-header-png"></div>
                        <div class="jp-header-jpeg"></div>
                    </div>
                        <div class="jp-header-fluid-object"></div>
                </div>
                <div class="jp-nav">
                	<div class="l"></div>
                	<div class="r"></div>
                	<ul class="jp-menu">
                		
                			<%if (session.getAttribute("tcdTracking").equals("1")){%>
					    	  <li>
					    	  	<a href="ViewServlet"><span class="l"></span><span class="r"></span><span class="t">TCD Tracking</span></a>                		
                			  </li>
					      	<%} %>
					      	<%if (session.getAttribute("rdTracking").equals("1")){%>
					    	  <li>
					    	  	<a href="ViewServletRD"><span class="l"></span><span class="r"></span><span class="t"></span><span class="t">RD Tracking</span></a>                		
                			  </li>
					      	<%} %>
					      	<%if (session.getAttribute("tcdBilling").equals("1")){%>
					    	  <li>
					    	  	<a href="TCDBillingServlet"><span class="l"></span><span class="r"></span><span class="t"></span><span class="t">TCD Billing</span></a>                		
                			  </li>
					      	<%} %>
					      	<%if (session.getAttribute("rdBilling").equals("1")){%>
					    	  <li>
					    	  	<a href="RDBilling"><span class="l"></span><span class="r"></span><span class="t"></span><span class="t">RD Billing</span></a>                		
                			  </li>
					      	<%} %>
					      	<%if (!(userId.equals("0"))){%>
					    	  <li>
					    	  	<a href="EditProfile.jsp"><span class="l"></span><span class="r"></span><span class="t"></span><span class="t">Edit profile</span></a>                		
                			  </li>
					      	<%} %>
                			
           		  		<li>
                			<a href="#" class="active"><span class="l"></span><span class="r"></span><span class="t">User Management</span></a>
							<ul>
								<%if (session.getAttribute("manageUsers").equals("1")){%>
						    		<li><a href="ManageAdminServlet">Manage users</a></li>
						      	<%} %>
						      	<%if (session.getAttribute("manageGroups").equals("1")){%>
					    			<li><a href="ManageGroupServlet">Manage groups</a></li>
					      		<%} %>
					      		<%if (session.getAttribute("userLog").equals("1")){%>
					    			<li><a href="UserLogServlet">User log</a></li>
					      		<%} %>
           					</ul>
               		  	</li>		
           		  	  	<li>
                			<a href="Logout"><span class="l"></span><span class="r"></span><span class="t">Logout</span></a>                		
                	  	</li>
               	  </ul>
                </div>
                <div class="jp-content-layout">
                  <div class="jp-post-body">
                     <h2 class="jp-postheader"> TCD Tracking</h2>
                      <div class="jp-postcontent">
                      <div id="AddAdmin">
                      <fieldset id="fsAddAdmin">
			            <legend class="pageTitleForm"><h3>Add user</h3></legend>
			            <form id="formAddAdmin" method="POST" class="form" >
			                <fieldset>
			                    <ol>
			                        <li class="">
			                            <div class="colTile">
			                              <label for="username">Username*</label>
			                            </div>
			                            <div class="colContent">
			                                <input type="text" name="username" id="username" value="" class="validate[required]" OnChange="JavaScript:checkUsername();" />
			                            	<span id="checkUser"></span>
			                            </div>
			                        </li>
			                        <li class="">
			                            <div class="colTile">
			                              <label for="password">Password*</label>
			                            </div>
			                            <div class="colContent">
			                                <input type="password" name="password" id="password" class="validate[required]"  value="" />
			                            </div>
			                        </li>
			                        <li class="">
			                            <div class="colTile">
			                              <label for="confirmpassword">Confirm Password*</label>
			                            </div>
			                            <div class="colContent">
			                                <input type="password" name="confirmpassword" id="confirmpassword" value=""  class="validate[required,equals[password]]"/>
			        
			                            </div>
			                        </li>
			                        <li class="">
			                            <div class="colTile">
			                                <label for="ddlGroup">Group*</label>
			                            </div>
			                            <div class="colContent">
			                                <select id="ddlGroup" name="ddlGroup" class="validate[required]" >
			                             
			                                </select>
			                            </div>
                        			</li>
                        			 <li class="">
			                            <div class="colTile">
			                              <label for="firstname">&nbsp;</label>
			                            </div>
			                            <div class="colContent">
			                              <input type="radio" id="rdoForm" name="rdoRecordType" value="Mr" checked><label>Mr.</label></input>
			                               <input type="radio" id="rdoForm" name="rdoRecordType" value="Mrs"><label>Mrs.</label></input>
			                               <input type="radio" id="rdoForm" name="rdoRecordType" value="Miss"><label>Miss</label></input>
			                               <input type="radio" id="rdoForm" name="rdoRecordType" value="Ms"><label>Ms.</label></input>
			                            </div>
			                        </li>
			                        <li class="column-2">
			                            <div class="colTile">
			                              <label for="firstname">First name</label>
			                            </div>
			                            <div class="colContent">
			                                <input type="text" name="firstname" id="firstname" value="" />
			                            </div>
			                        </li>
			                        <li class="column-2">
			                            <div class="colTile">
			                              <label for="lastname">Last name</label>
			                            </div>
			                            <div class="colContent">
			                                <input type="text" name="lastname" id="lastname" value="" />
			                            </div>
			                        </li>
			                        <li class="">
			                            <div class="colTile">
			                              <label for="address">Address</label>
			                            </div>
			                            <div class="colContent">
			                                <input type="text" name="address" id="address" value=""  class="width-100" />
			                            </div>
			                        </li>
			                        <li class="">
			                            <div class="colTile">
			                              <label for="tel">Tel.</label>
			                            </div>
			                            <div class="colContent">
			                                <input type="text" name="tel" id="tel" value="" />
			                            </div>
			                        </li>
			                        <li class="">
			                            <div class="colTile">
			                              <label for="fax">Fax.</label>
			                            </div>
			                            <div class="colContent">
			                                <input type="text" name="fax" id="fax" value="" />
			                            </div>
			                        </li>
			                        <li class="">
			                            <div class="colTile">
			                              <label for="mail">E-mail</label>
			                            </div>
			                            <div class="colContent">
			                                <input type="text" name="mail" id="mail" class="validate[custom[email]]" value="" />
			                            </div>
			                        </li>
			                        <li>
			                            <div  class="colTile">
			                                &nbsp;
			                            </div>
			                            <div class="colContent">
			                                <div id="btnCommand">
			                                  <button id="btnClear" type="reset" class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-icon-primary"><span class="ui-button-icon-primary ui-icon ui-icon-refresh"></span><span class="ui-button-text">Clear</span></button>
			                                   <button id="btnSave" type="button" class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-icon-primary" ><span class="ui-button-icon-primary ui-icon ui-icon-disk"></span><span class="ui-button-text">Save</span></button>
			                                </div>
			                            </div>
			                        </li>
			                    </ol>
			                </fieldset>
			            </form>
                            <div id="content-placeholder">
                                <div id="jqgrid">
                                    <table id="grdTransaction"></table>
                                    <div id="pagerTransaction"></div>
                                </div>
                            </div>
			        	</fieldset>  
			        	</div>                
			        	
                      </div>
                  </div>
                </div>
            </div>
        </div>
</div>
    
</body>	
<%
		}else{
%>
<script type="text/javascript">
    	window.location.href = "EditProfile";
</script>
<%
		}
	}else{
%>
<script type="text/javascript">
	window.location.href = "LoginServlet";
</script>
<%
	}
	
%>
</html>